<template>
	<view id="myIndex">
		<view class="status_bar">
			<view class="top_view"></view>  
		</view>  
		<view class="userTop">
			<view class="customerService s-flex-r-e-c" @click="callFn">
				<text>投诉热线</text>
				<view class="s-flex-c-c-c bgw ml10 call">
					<u-icon name="phone" color="#5C8FF8" size="24"></u-icon>
				</view>
			</view>
			<view class="user">
			    <image :src="userinfo.headImg" v-if='userinfo.headImg' @click="setHeadImg()" />
			    <image src="../../static/logo.png" v-else @click="setHeadImg()"/>
			    <view class="name">合作商: {{userinfo.name}}({{userinfo.agentname}})</view>
			</view>
			<view class="personal">
			    <view class="personalList personalList1">
			        <view class="num">{{homeData.totalMoney}}</view>
			        <view class="name">累计收益（元)</view>
			    </view>
			    <view class="personalList">
			        <view class="num">{{homeData.stockSize}}</view>
			        <view class="name">机器数量(台)</view>
			    </view>
			</view>
		</view>
		<view class="bodyBox">
			<view class="boxLine arrow" @tap="navTo('manager')">
				<view class="icon"><image src="../../static/my/user2.jpg"></view>
				<view class="data">客户经理</view>
			</view>
			<view class="boxLine arrow" @tap="navTo('info')">
				<view class="icon"><image src="../../static/my/user3.jpg"></view>
				<view class="data">我的信息</view>
			</view>
			<!-- 先注释 对的有问题 -->
<!-- 			<view class="boxLine arrow" @tap="navTo('/pages/index/problem')">
				<view class="icon"><image src="../../static/my/help.png"></view>
				<view class="data">常见问题</view>
			</view> -->
			<view class="boxLine arrow" @tap="navTo('/pages/index/notice')">
				<view class="icon"><image src="../../static/my/notice.png"></view>
				<view class="data">公告通知</view>
			</view>
			<view class="boxLine arrow" @tap="navTo('password')">
				<view class="icon"><image src="../../static/my/user4.jpg"></view>
				<view class="data">修改密码</view>
			</view>
			<view class="boxLine arrow" @tap="navTo('qrcode')">
				<view class="icon"><image src="../../static/my/user5.jpg"></view>
				<view class="data">我的二维码</view>
			</view>
			<view class="boxLine arrow" @tap="navTo('feedback')">
				<view class="icon"><image src="../../static/my/user6.jpg"></view>
				<view class="data">意见反馈</view>
			</view>
			<view class="boxLine arrow" @tap="clearStorage">
				<view class="icon"><image src="../../static/my/user7.jpg"></view>
				<view class="data">清理缓存<text>{{storageSize}}</text></view>
			</view>
			<view class="boxLine arrow" @tap="checkVersion">
				<view class="icon"><image src="../../static/my/user8.jpg"></view>
				<view class="data">版本更新<text>{{version}}</text></view>
			</view>
			<view class="boxLine arrow" @tap="navTo('closeUser')" v-if="iOS">
				<view class="icon"><image src="../../static/my/usera.png"></view>
				<view class="data">注销账户</view>
			</view>
			
			<view class="boxLine arrow" @tap="quite">
				<view class="icon"><image src="../../static/my/user9.jpg"></view>
				<view class="data">退出系统</view>
			</view>
			
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="success" cancelText="取消" confirmText="确定" title="提醒" :content="dialogContent" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
		<u-popup v-model="callDialog" mode="center" :closeable="true">
			<view class="call-box s-flex-c-s-c">
				<view class="call-item s-flex-r-c-c mb40" @click="$call('13803998543')">
					<text class="mr30">投诉热线1</text>
					<view class="s-flex-c-c-c call call2">
						<u-icon name="phone" color="#FFFFFF" size="24"></u-icon>
					</view>
					<text class="ml10">拨号</text>
				</view>
				<view class="call-item s-flex-r-c-c" @click="$call('13937152194')">
					<text class="mr30">投诉热线2</text>
					<view class="s-flex-c-c-c call call2">
						<u-icon name="phone" color="#FFFFFF" size="24"></u-icon>
					</view>
					<text class="ml10">拨号</text>
				</view>
			</view>
		</u-popup>
		<yomol-upgrade  :type="upgradeType" :url="upgradeUrl" title="发现新版本" :content="upgradeContent" ref="yomolUpgrade"></yomol-upgrade>
	</view>
</template>
<script>
	import APPUpdate from '@/common/appUpdate.js';
	import yomolUpgrade from '../../components/yomol-upgrade/yomol-upgrade.vue'
	export default {
		components: {
			yomolUpgrade
		},
		data() {
			return {
				flag:uni.getStorageSync("flag"),
				userinfo:uni.getStorageSync("userinfo"),
				homeData:{
					totalMoney:0,
					stockSize:0
				},
				
				version:'',
				headImg:'',
				
				storageSize:'',
				dialogContent:'',
				dialogType:'',
				upgradeType: 'pkg', //pkg 整包 wgt 升级包
				upgradeContent: '', //更新内容
				upgradeUrl: '', //更新地址
				callDialog:false,
				iOS:false,
			}
		},
		onShow() {
			this.getHomeData()
			this.getUserInfo()			
			//#ifdef APP-PLUS
			this.getStorageSize()
			this.getVersion()
			
			let os=plus.os.name
			if(os=='Android'){
				this.iOS=false
			}else{
				this.iOS=true
			}
			
			
			//#endif
		},
		methods: {
			callFn(){
				this.callDialog=true
			},
			setHeadImg(){
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: 'compressed',
					success: (chooseImageRes) => {
						let imgPath = chooseImageRes.tempFilePaths[0]
						this.httpClient.upload('agents/app/agent/uplodHeadImg',{
							name: 'file',
							filePath: imgPath
						}).then(res=>{
							if(res.code==200){
								
								that.headImg = res.data.src
								that.headImgSumbit()
							}
						})
					}
				})
			},
			headImgSumbit(){
				this.httpClient.post('agents/app/agent/editUserInfo',{
					headImg:this.headImg
				}).then(res=>{
					if(res.code==200){
						this.userinfo.headImg=this.headImg
						uni.showToast({
							icon: 'success',
							title: res.msg
						})
					}
				})
			},
			getHomeData(){
				this.httpClient.post('profit/app/profitSum/index', {}).then(res => {
					if(res.code==200){
						this.homeData.totalMoney=res.data.totalMoney
					}
				})
				this.httpClient.post('stock/app/stock/index', {}).then(res => {
					if(res.code==200){
						this.homeData.stockSize=res.data.total
					}
				})
			},
			getUserInfo(){
				this.httpClient.post('agents/app/agent/getUserInfoForApp', {}).then(res => {
					if(res.code==200){
						this.userinfo=res.data
					}
				})
			},
			getVersion(){
				// this.version=plus.runtime.version
				plus.runtime.getProperty(plus.runtime.appid, (wgtInfo) => {
					this.version=wgtInfo.version
				})
			},
			getStorageSize(){
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.storageSize = "0B";
					} else if (sizeCache < 1024) {
						that.storageSize = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						hat.storageSize = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.storageSize = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.storageSize = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				})
			},
			clearStorage(){
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					uni.showLoading({
						title:'清理中...'
					})
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径 
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							uni.hideLoading()
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录 
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000,
									})
									that.getStorageSize(); // 重新计算缓存 
									
								}, function(e) {
									console.log(e.message)
								})
							} else {
								entry.remove();
							}
						}, function(e) {
							uni.hideLoading()
							console.log('文件路径读取失败')
						});
					}
				} else { // ios
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.getStorageSize();
					});
				}
			},
			checkVersion() {
				APPUpdate()
			},
			navTo(url){
				uni.navigateTo({
					url:url
				})
			},
			quite(){
				this.dialogType='quite'
				this.dialogContent="确定要退出吗？"
				this.$refs.alertDialog.open()
			},
			clearCach(){
				this.dialogType='clearCach'
				this.dialogContent="确定要清理吗？"
				this.$refs.alertDialog.open()
			},
			confirm(){
				if(this.dialogType=='quite'){
					this.setQuite()
				}else if(this.dialogType=='clearCach'){
					this.clearStorage()
				}
				
			},
			//确定退出
			setQuite() {
				//uni.clearStorageSync();
				uni.removeStorageSync("access_token")
				uni.showToast({
					icon: 'success',
					duration: 1000,
					title: '退出成功'
				});
				setTimeout(function() {
					uni.reLaunch({
						url: '/pages/common/login'
					});
				}, 1500);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.call{
		border-radius: 50%;
		padding: 6rpx;
	}
	.call2{
		border: 3rpx solid #FFF;
		box-sizing: border-box;
	}
	.call-box{
		width: 700rpx;
		padding: 80rpx 0;
		box-sizing: border-box;
		border-radius: 8rpx;
		.call-item{
			padding: 10rpx 0;
			width: 380rpx;
			background: #1C9CFB;
			border-radius: 6rpx;
			color: #fff;
		}
	}
	#myIndex {
		height:100%;
		
		.status_bar{
			height: var(--status-bar-height);
			width: 100%;
			.top_view {  
			    height: var(--status-bar-height);  
			    width: 100%;  
			    position: fixed;  
			    background-color: #508DF4; 
			    top: 0;  
			    z-index: 999;  
			}
		}
		
		.userTop {
		    height: 450rpx;
		    background-image: url('../../static/my/barBg.png');
		    background-size: 100% 100%;
		    padding: 100rpx 50rpx 0;
		    color: #fff;
			position: relative;
			.user{
				display: flex;
				align-items: center;
				image{
					width: 4rem;
					height: 4rem;
					border-radius: 50%;
					margin-right: 20rpx;
				}
			}
			.personal {
			    width: 100%;
			    margin-top: 0.25rem;
			    display: flex;
			    align-items: center;
				.personalList{
					flex: 1;
					font-size: 0.75rem;
					text-align: center;
					position: relative;
					.num{
						font-size: 1.2rem;
					}
				}
				.personalList1:before{
					content:'';
					width: 1px;
					height: 1.2rem;
					position: absolute;
					top: 50%;
					right: 00rem;
					margin-top: -0.6rem;
					background: #fff;
				}
			}
		}
		.bodyBox{
			margin:20rpx;
			padding:30rpx;
			border-radius: 10rpx;
			background: #FFF;
			position: relative;
			.boxLine{
				border-bottom:1rpx solid #f7f7f7;display:flex;
				align-items: center;
				justify-content: center;
				padding:20rpx 0;
				.icon{
					width:60rpx;height:60rpx;
					image{
						width:100%;height:100%;
					}
				}
				.data{
					flex:1;
					margin-left:20rpx;
					text{
						position: absolute;
						right:0;
						padding-right:30rpx;
					}
				}
			}
		}
		.bodyBox .boxLine:last-child{
			border-bottom:none;
		}
		.qrBox{
			width:500rpx;height:680rpx;;
			image.code{
				width:500rpx;height:500rpx;
			}
			.textBox{
				display:flex;text-align:center;
				view{
					height:100%;flex:1;background:#FFA500;color:#FFF;padding:15rpx;
				}
				view.Left{
					border-top-left-radius: 10rpx;
					border-bottom-left-radius: 10rpx;
					margin-right:5rpx;
				}
				view.Right{
					border-top-right-radius: 10rpx;
					border-bottom-right-radius: 10rpx;
					margin-left:5rpx;
				}
			}
			.close{
				height:60rpx;text-align: center;margin:50rpx 0;
				image{
					width:50rpx;height:50rpx;
				}
			}
			
		}
	}
</style>